package com.cswnode.Controller;

import com.cswnode.Entity.Comment;
import com.cswnode.Entity.DiscussPost;
import com.cswnode.Entity.Page;
import com.cswnode.Entity.User;
import com.cswnode.Service.CommentService;
import com.cswnode.Service.DiscussPostService;
import com.cswnode.Service.LikeService;
import com.cswnode.Service.UserService;
import com.cswnode.Util.CommunityUtil;
import com.cswnode.Util.Constant;
import com.cswnode.Util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author csw
 * @create 2020/9/22 13:41
 */
@Controller
public class DiscussPostController {
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private LikeService likeService;

    //发布帖子
    @PostMapping("/addDiscussPost")
    @ResponseBody
    public String addDiscussPost(String title,String content){
        User user = hostHolder.getUser();
        if (user == null){
            return CommunityUtil.getJsonString(403,"用户暂未登陆，请登录！");
        }
        DiscussPost post = new DiscussPost();
        post.setTitle(title);
        post.setContent(content);
        post.setUserId(user.getId());
        post.setCreateTime(new Date());

        int i = discussPostService.insertDiscussPost(post);
        if (i == 0){
            return CommunityUtil.getJsonString(403,"帖子发布失败！");
        }
        return CommunityUtil.getJsonString(0,"发布成功！");

    }
    //查看帖子详情
    @GetMapping("/findDiscussPost/{id}")
    public String findDiscussPost(@PathVariable("id")int id, Model model, Page page){
        if (hostHolder.getUser() == null){
            return "/site/login";
        }
        int CurrentUserId = hostHolder.getUser().getId();
        DiscussPost post = discussPostService.findDiscussPostById(id);
        if (post == null){
            return "/site/login";
        }
        model.addAttribute("post",post);
        User user = userService.findUserById(post.getUserId());
        model.addAttribute("user",user);
        model.addAttribute("LikeCount",
                likeService.findEntityLikeCount(Constant.ENTITY_TYPE_POST,post.getId()));
        model.addAttribute("LikeStatus",
                likeService.findEntityLikeStatus(CurrentUserId,Constant.ENTITY_TYPE_POST,post.getId()));
        //评论分页信息
        page.setLimit(5);
        page.setRows(post.getCommentCount());
        page.setPath("/findDiscussPost/"+post.getId());
        //评论列表）给帖子的评论
        List<Comment> commentByLimit = commentService.findCommentByLimit(post.getId(),Constant.ENTITY_TYPE_POST,
                page.getOffset(), page.getLimit());

        //返回前端的评论列表
        List<Map<String,Object>> CommentVoList = new ArrayList<>();
        if (commentByLimit != null){
            for (Comment comment : commentByLimit) {
                //评论的Vo
                Map<String,Object> Commentmap = new HashMap<>();
                //添加评论
                Commentmap.put("comment",comment);
                //添加评论对应的作者
                Commentmap.put("user",userService.findUserById(comment.getUserId()));
                Commentmap.put("LikeCount",
                        likeService.findEntityLikeCount(Constant.ENTITY_TYPE_COMMENT,comment.getId()));
                Commentmap.put("LikeStatus",likeService.findEntityLikeStatus(CurrentUserId,
                        Constant.ENTITY_TYPE_COMMENT,comment.getId()));


                //回复列表）给帖子里的评论的回复评论；不分页
                List<Comment> replyList = commentService.findCommentByLimit(comment.getId(), Constant.ENTITY_TYPE_COMMENT,
                        0, Integer.MAX_VALUE);
                //回复VO列表
                List<Map<String,Object>> replyListVoList = new ArrayList<>();
                if (replyList != null){
                    for (Comment reply : replyList) {
                        Map<String,Object> replyMap = new HashMap<>();
                        replyMap.put("reply",reply);
                        replyMap.put("user",userService.findUserById(reply.getUserId()));
                        replyMap.put("LikeCount",
                                likeService.findEntityLikeCount(Constant.ENTITY_TYPE_COMMENT,reply.getId()));
                        replyMap.put("LikeStatus",
                                likeService.findEntityLikeStatus(CurrentUserId,Constant.ENTITY_TYPE_COMMENT,reply.getId()));
                        //回复的目标用户
                        User target = reply.getTargetId() == 0 ? null : userService.findUserById(reply.getTargetId());
                        replyMap.put("target",target);
                        replyListVoList.add(replyMap);
                    }
                }
                Commentmap.put("replys",replyListVoList);
                int num = commentService.findNumByEntity(comment.getId(),Constant.ENTITY_TYPE_COMMENT);
                //评论的评论数量
                Commentmap.put("replyNum",num);
                CommentVoList.add(Commentmap);
            }

        }
        model.addAttribute("comments",CommentVoList);
        return "/site/discuss-detail";
    }

}
